﻿using System;
using System.Data.Entity;
using System.Data.Entity.Validation;
using SuppKids.Core.Domain;

namespace SuppKids.Core.Data
{
	public class ApplicationDBContext : DbContext, IEntityDbContext
	{
		public ApplicationDBContext()
		{
			this.Configuration.LazyLoadingEnabled = true;
		}

		public ApplicationDBContext(string databasePath)
			: base(string.Format("Data Source={0};Persist Security Info=False;", databasePath))
		{
			this.Configuration.LazyLoadingEnabled = true;
		}

		public DbSet<DailyMenu> DailyMenus { get; set; }
		public DbSet<Dish> Dishes { get; set; }
		public DbSet<Product> Products { get; set; }
		public DbSet<Ingredient> ProductConsumptions { get; set; }
		public DbSet<ProductAtStorage> ProductsAtStorage { get; set; }

        public DbSet<Norm> Norms { get; set; }

		//public DbSet<Log> Logs { get; set; }

		public void Commit()
		{
			try
			{
				this.SaveChanges();
			}
			catch (DbEntityValidationException exception)
			{
                //MessageBox.Show(Messages.ProductIsNotSelected); 
                throw;
			}

		}

		protected override void OnModelCreating(DbModelBuilder modelBuilder)
		{
			modelBuilder.Entity<Ingredient>()
				.HasRequired(a => a.Dish)
				.WithMany(a => a.Ingredients)
				.WillCascadeOnDelete(false);
			modelBuilder.Entity<Ingredient>()
				.HasRequired(a => a.Product)
				.WithMany(a => a.Ingredients)
				.WillCascadeOnDelete(false);

		}
	}
}